package com.google.android.exoplayer2.util;

import android.annotation.TargetApi;
import android.graphics.SurfaceTexture;
import android.graphics.SurfaceTexture.OnFrameAvailableListener;
import android.opengl.EGL14;
import android.opengl.EGLConfig;
import android.opengl.EGLContext;
import android.opengl.EGLDisplay;
import android.opengl.EGLSurface;
import android.opengl.GLES20;
import android.os.Handler;
import androidx.annotation.Nullable;
import o.amm;
import o.anr;

@TargetApi(17)
public final class EGLSurfaceTexture implements OnFrameAvailableListener, Runnable {
    private static final int[] ˎ = new int[]{12352, 4, 12324, 8, 12323, 8, 12322, 8, 12321, 8, 12325, 0, 12327, 12344, 12339, 4, 12344};
    @Nullable
    private SurfaceTexture ʻ;
    @Nullable
    private EGLContext ʽ;
    private final Handler ˊ;
    @Nullable
    private EGLDisplay ˋ;
    @Nullable
    private final a ˏ;
    private final int[] ॱ;
    @Nullable
    private EGLSurface ᐝ;

    public static final class GlException extends RuntimeException {
        private GlException(String str) {
            super(str);
        }
    }

    public interface a {
        void ˏ();
    }

    public EGLSurfaceTexture(Handler handler) {
        this(handler, null);
    }

    public EGLSurfaceTexture(Handler handler, @Nullable a aVar) {
        this.ˊ = handler;
        this.ˏ = aVar;
        this.ॱ = new int[1];
    }

    public void ˏ(int i) {
        this.ˋ = ˎ();
        EGLConfig ˋ = ˋ(this.ˋ);
        this.ʽ = ˏ(this.ˋ, ˋ, i);
        this.ᐝ = ˋ(this.ˋ, ˋ, this.ʽ, i);
        ॱ(this.ॱ);
        this.ʻ = new SurfaceTexture(this.ॱ[0]);
        this.ʻ.setOnFrameAvailableListener(this);
    }

    public void ˋ() {
        this.ˊ.removeCallbacks(this);
        try {
            if (this.ʻ != null) {
                this.ʻ.release();
                GLES20.glDeleteTextures(1, this.ॱ, 0);
            }
            if (!(this.ˋ == null || this.ˋ.equals(EGL14.EGL_NO_DISPLAY))) {
                EGL14.eglMakeCurrent(this.ˋ, EGL14.EGL_NO_SURFACE, EGL14.EGL_NO_SURFACE, EGL14.EGL_NO_CONTEXT);
            }
            if (!(this.ᐝ == null || this.ᐝ.equals(EGL14.EGL_NO_SURFACE))) {
                EGL14.eglDestroySurface(this.ˋ, this.ᐝ);
            }
            if (this.ʽ != null) {
                EGL14.eglDestroyContext(this.ˋ, this.ʽ);
            }
            if (anr.ˋ >= 19) {
                EGL14.eglReleaseThread();
            }
            if (!(this.ˋ == null || this.ˋ.equals(EGL14.EGL_NO_DISPLAY))) {
                EGL14.eglTerminate(this.ˋ);
            }
            this.ˋ = null;
            this.ʽ = null;
            this.ᐝ = null;
            this.ʻ = null;
        } catch (Throwable th) {
            if (!(this.ˋ == null || this.ˋ.equals(EGL14.EGL_NO_DISPLAY))) {
                EGL14.eglMakeCurrent(this.ˋ, EGL14.EGL_NO_SURFACE, EGL14.EGL_NO_SURFACE, EGL14.EGL_NO_CONTEXT);
            }
            if (!(this.ᐝ == null || this.ᐝ.equals(EGL14.EGL_NO_SURFACE))) {
                EGL14.eglDestroySurface(this.ˋ, this.ᐝ);
            }
            if (this.ʽ != null) {
                EGL14.eglDestroyContext(this.ˋ, this.ʽ);
            }
            if (anr.ˋ >= 19) {
                EGL14.eglReleaseThread();
            }
            if (!(this.ˋ == null || this.ˋ.equals(EGL14.EGL_NO_DISPLAY))) {
                EGL14.eglTerminate(this.ˋ);
            }
            this.ˋ = null;
            this.ʽ = null;
            this.ᐝ = null;
            this.ʻ = null;
        }
    }

    public SurfaceTexture ॱ() {
        return (SurfaceTexture) amm.ॱ(this.ʻ);
    }

    public void onFrameAvailable(SurfaceTexture surfaceTexture) {
        this.ˊ.post(this);
    }

    public void run() {
        ˊ();
        if (this.ʻ != null) {
            try {
                this.ʻ.updateTexImage();
            } catch (RuntimeException e) {
            }
        }
    }

    private void ˊ() {
        if (this.ˏ != null) {
            this.ˏ.ˏ();
        }
    }

    private static EGLDisplay ˎ() {
        EGLDisplay eglGetDisplay = EGL14.eglGetDisplay(0);
        if (eglGetDisplay == null) {
            throw new GlException("eglGetDisplay failed");
        }
        int[] iArr = new int[2];
        if (EGL14.eglInitialize(eglGetDisplay, iArr, 0, iArr, 1)) {
            return eglGetDisplay;
        }
        throw new GlException("eglInitialize failed");
    }

    private static EGLConfig ˋ(EGLDisplay eGLDisplay) {
        EGLConfig[] eGLConfigArr = new EGLConfig[1];
        int[] iArr = new int[1];
        if (EGL14.eglChooseConfig(eGLDisplay, ˎ, 0, eGLConfigArr, 0, 1, iArr, 0) && iArr[0] > 0 && eGLConfigArr[0] != null) {
            return eGLConfigArr[0];
        }
        throw new GlException(anr.ˏ("eglChooseConfig failed: success=%b, numConfigs[0]=%d, configs[0]=%s", Boolean.valueOf(r0), Integer.valueOf(iArr[0]), eGLConfigArr[0]));
    }

    private static EGLContext ˏ(EGLDisplay eGLDisplay, EGLConfig eGLConfig, int i) {
        int[] iArr;
        if (i == 0) {
            iArr = new int[]{12440, 2, 12344};
        } else {
            iArr = new int[]{12440, 2, 12992, 1, 12344};
        }
        EGLContext eglCreateContext = EGL14.eglCreateContext(eGLDisplay, eGLConfig, EGL14.EGL_NO_CONTEXT, iArr, 0);
        if (eglCreateContext != null) {
            return eglCreateContext;
        }
        throw new GlException("eglCreateContext failed");
    }

    private static EGLSurface ˋ(EGLDisplay eGLDisplay, EGLConfig eGLConfig, EGLContext eGLContext, int i) {
        EGLSurface eGLSurface;
        if (i == 1) {
            eGLSurface = EGL14.EGL_NO_SURFACE;
        } else {
            int[] iArr;
            if (i == 2) {
                iArr = new int[]{12375, 1, 12374, 1, 12992, 1, 12344};
            } else {
                iArr = new int[]{12375, 1, 12374, 1, 12344};
            }
            eGLSurface = EGL14.eglCreatePbufferSurface(eGLDisplay, eGLConfig, iArr, 0);
            if (eGLSurface == null) {
                throw new GlException("eglCreatePbufferSurface failed");
            }
        }
        if (EGL14.eglMakeCurrent(eGLDisplay, eGLSurface, eGLSurface, eGLContext)) {
            return eGLSurface;
        }
        throw new GlException("eglMakeCurrent failed");
    }

    private static void ॱ(int[] iArr) {
        GLES20.glGenTextures(1, iArr, 0);
        int glGetError = GLES20.glGetError();
        if (glGetError != 0) {
            throw new GlException("glGenTextures failed. Error: " + Integer.toHexString(glGetError));
        }
    }
}
